import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/login")
public class IoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String userName = req.getParameter("user");
        String password = req.getParameter("password");

        if(!("zhangsan".equals(userName))|| !("123".equals(password))) {
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前的用户名或者密码错误!");
            String str = "username: " + userName + " password: " + password;
            resp.getWriter().write(str);
            return;
        }
        HttpSession session = req.getSession(true);
        session.setAttribute("userName",userName);
        session.setAttribute("longTime",System.currentTimeMillis());
        resp.setContentType("text/html; charset=utf8");
        resp.sendRedirect("index");
    }
}
